﻿#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using System.Drawing;
using System.IO;
using ZO.SmartCore.Helpers;
using ArgumentNullException= ZO.SmartCore.Core.ArgumentNullException;
using ArgumentOutOfRangeException= ZO.SmartCore.Core.ArgumentOutOfRangeException;
using ZO.SmartCore.My4CSharp;

#endregion

namespace ZO.SmartCore.Plugins
{
    /// <summary>
    /// Wrapper class for Image to Set Plugin logo
    /// </summary>
    [Serializable()]
    public sealed class PluginImage
    {

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PluginImage"/> class with the path of the image file to load.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        public PluginImage(string fileName)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            innerImage = That.Computer.FileSystem.ReadAllBytes(fileName);
            

            using (Image img = Image.FromStream(new MemoryStream(innerImage)))
            {
                this._Height = img.Height;
                this._Width = img.Width;
            }
        } // Image


        /// <summary>
        /// Initializes a new instance of the <see cref="PluginImage"/> class with the image bytes to load.
        /// </summary>
        /// <param name="imageBytes">The image bytes.</param>
        public PluginImage(byte[] imageBytes)
        {
            if (imageBytes == null)
            {
                throw new ArgumentNullException("imageBytes");
            }

            if (imageBytes.Length <= 0)
            {
                throw new ArgumentOutOfRangeException("imageBytes");

            }

            innerImage = ByteHelper.Clone(imageBytes);



            using (Image img = Image.FromStream(new MemoryStream(innerImage)))
            {
                this._Height = img.Height;
                this._Width = img.Width;
            }
        } // Image


        /// <summary>
        /// Initializes a new instance of the <see cref="PluginImage"/> class with the path of the image file to load.
        /// </summary>
        /// <param name="image">The image.</param>
        public PluginImage(Image image)
        {
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }

            
            innerImage = ImageHelper.GetBytesFromImage(image);


            using (Image img = Image.FromStream(new MemoryStream(innerImage)))
            {
                this._Height = img.Height;
                this._Width = img.Width;
            }
        } // Image
        #endregion

        #region Destructor

        #endregion

        #region Fields
        private int _Width;
        private int _Height;

        private byte[] innerImage;

        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties
        /// <summary>
        /// Gets the image in bytes.
        /// </summary>
        /// <returns></returns>
        public byte[] GetBytes()
        {
            return innerImage;
        } // GetBytes

        /// <summary>
        /// Gets the width, in pixels, of this Image. 
        /// </summary>
        public int Width
        {
            get
            {
                return _Width;
            } // get
        }

        /// <summary>
        /// Gets the Height , in pixels, of this Image. 
        /// </summary>
        public int Height
        {
            get
            {
                return _Height;
            } // get
        }
        #endregion

        #region Methods

        #endregion
    }
}
